﻿Public NotInheritable Class SuperRelationFactory
    Private Shared _factory As SuperRelationFactory = Nothing

    Public Shared Function Instance() As SuperRelationFactory
        '#97　MySQL対応
        'DBMSごとの生成クラスを統合。
        If _factory Is Nothing Then
            _factory = New SuperRelationFactory
        End If
        Return _factory
    End Function

    Public Function Create(atr As TableAttribute) As List(Of SuperRelation)
        Dim lst As New List(Of SuperRelation)
        If atr.IsRootBase Then Return lst

        '基底クラスへのカスケード
        '＃96　「Nリレーションクラスを継承するとDDLに失敗する」を修正
        For Each item As ColumnAttribute In (From el In ColumnAttributeReader.ReadPrimaryKeys(atr.ModelType.BaseType) Where el.IsLoadKey)
            Dim myColumn As ColumnAttribute = Nothing
            Dim yourColumn As ColumnAttribute = Nothing

            If item.IsParent Then
                '基底クラスの主キーが親の場合に対応
                yourColumn = DirectCast(item, ParentRelationColumnAttribute).YourColumn

                myColumn = New ParentRelationColumnAttribute(item.PropertyInfo, yourColumn.PlainColumnName, yourColumn, False)
                myColumn.Key = New KeyAttribute
            Else
                yourColumn = item

                myColumn = New ColumnAttribute(yourColumn)
                myColumn.Key = New KeyAttribute
            End If

            lst.Add(New SuperRelation(myColumn, item))
        Next

        Return lst
    End Function

End Class
